#include "Circle.h"

namespace custom{

constexpr float PI = 3.14159265358979323846;

float Circle::solvePerimeter()
{
    this->perimeter = 2 * PI * r;
    return this->perimeter;
}

float Circle::solveArea()
{
    this->area = PI * r * r;
    return this->area;
}

float operator&(const Circle& c1, const Circle& c2)
{
    //计算c1和c2相交部分的面积
    float result = 0;

    //计算圆心距离
    float distance = sqrt(pow(c1.center.x  - c2.center.x, 2) + pow(c1.center.y  - c2.center.y, 2));
    //如果两圆半径之和大于两圆距离，则两圆相交
    if(c1.r + c2.r > distance)
    {   
        //实现两圆面积交运算
        float theta1;
        float theta2;
        float s1;
        float s2;

        theta1 = acos((c1.r*c1.r + distance*distance - c2.r*c2.r) / (2*c1.r*distance));
        theta2 = acos((c2.r*c2.r + distance*distance - c1.r*c1.r) / (2*c2.r*distance));

        s1 = c1.r*c1.r * (theta1 - 0.5 * sin(2*theta1));
        s2 = c2.r*c2.r * (theta2 - 0.5 * sin(2*theta2));

        result = s1 + s2;
    }
    else if(c1.r + c2.r == distance)
    {
        //两圆相切
        return 0;
    }
    else if(fabs(c1.r - c2.r) == distance)
    {
        //两圆相切
        return 0;
    }
    else if(c1.r + c2.r < distance)
    {
        //两圆相离
        return 0;
    }
    else
    {
        //包含关系
        float r = fmin(c1.r, c2.r);
        
        result = PI * r * r;
    }
    return result;
}

float operator|(const Circle& c1, const Circle& c2)
{
    float area1 = c1.r * c1.r * PI;
    float area2 = c2.r * c2.r * PI;
    return (area1 + area2 - (c1 & c2));
}

}